var modal = weex.requireModule('modal');
var showConfirm = function(content,callback){
	modal.confirm({
		message: content,
		okTitle: '确认',
		cancelTitle: '取消',
		duration: 0.3
	}, function(value){
		if(callback) {
			callback(value);
		}
	});
};

var showToast= function(content){
	modal.toast({
		message: content,
		duration: 0.3
	});
};

var showAlert = function(content,callback){
	modal.alert({
		message:  content,
		okTitle: '确认',
		duration: 0.3
	},  function(value){
		if(callback) {
			callback(value);
		}
	});
};

//设置token
var getToken = function(){
	$.ajax({
		url:  _contextpath + '/common/ajax/token',
		type: 'GET',
		data: {},
		async:false,
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With","XMLHttpRequest");
		},
		success: function(data){
			_webtoken = data;
		},
	});
};

//get类型ajax请求
var getAjax = function(url,obj,async,callback){
	$.ajax({
		url: _webpath + url,
		type: 'GET',
		data: obj,
		async:async,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With","XMLHttpRequest");
		},
		success: function (data) {
			try{
				data = JSON.parse(data);
			}catch(e){

			}
			if(data.messageType=='SUCCESS'){
				if(data.messageContent){
					showToast(data.messageContent);
				}
			}
			else if(data.messageType=='FAIL'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType=='WARN'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}

			if(data.messageContent && data.messageContent!='' && data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				setTimeout(function(){
					location.href=data.redirectUrl;
				},1000);
			}
			else if(data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				location.href=data.redirectUrl;
			}
			if(callback){
				callback(data);
			}
		},
		error: function(data){
			data = data.responseText;
			try{
				data = JSON.parse(data);
			}catch(e){

			}
			if(data.messageType=='SUCCESS'){
				if(data.messageContent){
					showToast(data.messageContent);
				}
			}
			else if(data.messageType=='FAIL'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType=='WARN'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			if(data.messageContent && data.messageContent!='' && data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				setTimeout(function(){
					 location.href=data.redirectUrl;
				},1000);
			}
			else if(data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				 location.href=data.redirectUrl;
			}
		}
	});
};

//post类型ajax请求，数据以form-data类型发送
//**未进主页时启用此方法
var initFormAjax = function(url,obj,async,type,callback){
	$.ajax({
		url: _webpath + url,
		type: type,
		data: obj,
		async:async,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		beforeSend: function(request) {
			request.setRequestHeader("X-Requested-With","XMLHttpRequest");
		},
		success: function (data) {
			if(typeof data=='string'){
				try{
					data = JSON.parse(data);
				}catch(e){

				}
			}
			var redirectUrl=location.href;
			if(data.messageContent && data.messageContent!='' && data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				setTimeout(function(){
					if(redirectUrl.indexOf('?src=')>-1){
						redirectUrl = decodeURIComponent(location.href);
						redirectUrl = redirectUrl.substring(redirectUrl.indexOf('?src=')+5,redirectUrl.length);
						if(redirectUrl){
							location.href=redirectUrl;
						}
					}
					else if(redirectUrl.indexOf('?src=')==-1){
						location.href=data.redirectUrl;
					}
				},1000);
			}
			else if(data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				if(redirectUrl.indexOf('?src=')>-1){
					redirectUrl = decodeURIComponent(redirectUrl);
					redirectUrl = redirectUrl.substring(redirectUrl.indexOf('?src=')+5,redirectUrl.length);
					if(redirectUrl){
						location.href=redirectUrl;
					}
				}
				else if(redirectUrl.indexOf('?src=')==-1){
					location.href=data.redirectUrl;
				}
			}
			if(callback){
				callback(data);
			}
		},
		error: function(data){
			dataSource = data.responseText;
			try{
				dataSource = JSON.parse(dataSource);
			}catch(e){

			}
			if(dataSource.messageContent && dataSource.messageContent!='' && dataSource.redirectUrl && dataSource.redirectUrl!='' && dataSource.redirectUrl!=null){
				setTimeout(function(){
					location.href=dataSource.redirectUrl;
				},1000);
			}
			else if(dataSource.redirectUrl && dataSource.redirectUrl!='' && dataSource.redirectUrl!=null){
				 location.href=dataSource.redirectUrl;
			}
			if(callback){
				callback(dataSource,data);
			}
		}
	});
};
//post类型ajax请求，数据以form-data类型发送
var formAjax = function(url,obj,async,callback){
	getToken();
	$.ajax({
		url: _webpath + url,
		type: 'POST',
		data: obj,
		async:async,
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		beforeSend: function(request){
			request.setRequestHeader('_webtoken',_webtoken);
			request.setRequestHeader("X-Requested-With","XMLHttpRequest");
		},
		success: function (data) {
			//重新获取token
			//getToken();
			try{
				data = JSON.parse(data);
			}catch(e){

			}
			if(data.messageType=='SUCCESS'){
				if(data.messageContent){
					showToast(data.messageContent);
				}
			}
			else if(data.messageType=='FAIL'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType=='WARN'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType==process){
				setTimeout(function(){formAjax(url,obj,async,callback)},5000);
			}
			if(callback){
				callback(data);
			}
			if(data.messageContent && data.messageContent!='' && data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				setTimeout(function(){
					location.href=data.redirectUrl;
				},1000);
			}
			else if(data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				location.href=data.redirectUrl;
			}
		}.bind(this),
		error: function(data){
			//parent.errorMessage(url + '请求异常');
			//重新获取token
		//	getToken();
			data = data.responseText;
			try{
				data = JSON.parse(data);
			}catch(e){

			}
			if(data.messageType=='SUCCESS'){
				if(data.messageContent){
					showToast(data.messageContent);
				}
			}
			else if(data.messageType=='FAIL'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType=='WARN'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			if(data.messageContent && data.messageContent!='' && data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				setTimeout(function(){
					location.href=data.redirectUrl;
				},1000);
			}
			else if(data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				//window.location.href=data.redirectUrl;
				location.href=data.redirectUrl;
			}
		}
	});
};

//post类型ajax请求，数据以json类型发送
var jsonAjax = function(url,obj,async,callback,sureFlag){
	getToken();
	$.ajax({
		url: _webpath + url,
		type: 'POST',
		data: JSON.stringify(obj),
		dataType: 'json',
		contentType: 'application/json',
		async:async,
		
		beforeSend: function(request){
			request.setRequestHeader('_webtoken',_webtoken);
			request.setRequestHeader("X-Requested-With","XMLHttpRequest");
		},
		success: function (data) {
			//重新获取token
		//	getToken();
			try{
				data = JSON.parse(data);
			}catch(e){

			}
			if(data.messageType=='SUCCESS'){
				if(data.messageContent){
					showToast(data.messageContent);
				}
			}
			else if(data.messageType=='FAIL'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType=='WARN'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType==process){
				setTimeout(function(){jsonAjax(url,obj,async,callback)},5000);
			}

			if(data.messageContent && data.messageContent!='' && data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null && !sureFlag){
				setTimeout(function(){
					location.href=data.redirectUrl;

				},1000);
			}
			else if(data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null && !sureFlag){
				if(data.valueObj.title){
					location.href=data.redirectUrl;
				}
			}
			if(callback){
				callback(data);
			}
		}.bind(this),
		error: function(data){
			//parent.errorMessage(url + '请求异常');
			//重新获取token
			//getToken();
			data = data.responseText;
			try{
				data = JSON.parse(data);
			}catch(e){

			}
			if(data.messageType=='SUCCESS'){
				if(data.messageContent){
					showToast(data.messageContent);
				}
			}
			else if(data.messageType=='FAIL'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			else if(data.messageType=='WARN'){
				if(data.messageContent){
					showAlert(data.messageContent);
				}
			}
			if(data.messageContent && data.messageContent!='' && data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				setTimeout(function(){
					location.href=data.redirectUrl;
				},1000);
			}
			else if(data.redirectUrl && data.redirectUrl!='' && data.redirectUrl!=null){
				location.href=data.redirectUrl;
			}
			if(callback){
				callback(data);
			}
		}
	});
};

//上传文件
var myUpload = function(url,obj,file,callback){
	var fd = new FormData(),
		xhr = new XMLHttpRequest();
	var fileList = file;
	for(var i in fileList){
		if(Object.prototype.toString.call(fileList[i]) === '[object Array]') {
			for(var j = 0; j < fileList[i].length;j++) {
				fd.append(i, fileList[i][j]);
			}
		}
		else {
			fd.append(i, fileList[i]);
		}

	}

	for(var k in obj){
		fd.append(k, obj[k]);
	}
	getToken();
	xhr.open('post', _webpath+ url);
	xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
	xhr.setRequestHeader('_webtoken',_webtoken);
	xhr.onreadystatechange = function(){
		if(xhr.status == 200){
			if(xhr.readyState == 4){
				if(callback instanceof Function){
					callback(xhr.responseText);
				}
			}
		}
	//	getToken();
	};
	xhr.send(fd);
};


